package com.bandit.utils;

import lombok.Data;

import java.util.List;

@Data
public class PageUtils<T> {

    private long pageIndex;  //当前页码  page
    private long pageSize;  //页面大小   limit
    private long totalCount; //总条数    List里面总数据数
    private long pageCount;  //当前页数里面有几条

    private List<T> records; //每页的数据集合

    private long numberStart = 1;//开始的页码序号
    private long numberEnd;//结束序号

    /**
     *
     * @param totalList 外部传入的list
     * @param pageIndex page号，第几页
     * @param pageSize  limit，每页的容量
     */
    public PageUtils(List<T> totalList,int pageIndex,int pageSize){
        this.totalCount = totalList.size();
        if (totalCount == 0){
            records = null;
        }else {
            records = pageResult(totalList,pageIndex,pageSize);
        }
    }

    public List<T> pageResult(List<T> totalList,int pageIndex,int pageSize){
        long count = totalCount / ((long) pageIndex * pageSize);
        System.out.println(count);
        if(count>0){//大于0说明不是最后
            return totalList.subList((pageIndex-1)*pageSize,pageIndex*pageSize);
        }else{//最后一页
            long lastCount = totalCount%((count+1)*pageSize);  //剩下的页数
            return totalList.subList((pageIndex-1)*pageSize, (int) ((pageIndex-1)*pageSize+lastCount));
        }
    }

}
